home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MEMORY.SWG / 0027_Loading a file on HEAP.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  2KB  |  70 lines

  1. {
  2. GUY MCLOUGHLIN
  3.  
  4. >How would I load a file straight into memory, and access it directly
  5. >using pointers?
  6.  
  7. Load file data onto the HEAP memory-pool.
  8. }
  9.  
  10. program LoadFileOnHEAP;
  11.  
  12. type
  13.   { Array type used to define the data buffer. }
  14.   arby_60K   = array[1..61440] of byte;
  15.   { Pointer type used to allocate the data buffer on the HEAP memory pool. }
  16.   po_60KBuff = ^arby_60K;
  17.  
  18. const
  19.   { Buffer size in bytes constant. }
  20.   co_BuffSize = sizeof(arby_60K);
  21.  
  22. { Check for IO errors, close data file if necessary. }
  23. procedure CheckForErrors(var fi_Temp : file; bo_CloseFile : boolean);
  24. var
  25.   by_Temp : byte;
  26. begin
  27.   by_Temp := ioresult;
  28.   if (by_Temp <> 0) then
  29.   begin
  30.     writeln('FILE ERROR = ', by_Temp);
  31.     if bo_CloseFile then
  32.       close(fi_Temp);
  33.     halt(1)
  34.   end
  35. end;
  36.  
  37. var
  38.   wo_BuffIndex,
  39.   wo_BytesRead : word;
  40.   po_Buffer    : po_60KBuff;
  41.   fi_Temp      : file;
  42.  
  43. BEGIN
  44.   assign(fi_Temp, 'EE.PAS');
  45.   {$I-}
  46.   reset(fi_Temp, 1);
  47.   {$I+}
  48.   CheckForErrors(fi_Temp, false);
  49.  
  50.   { Check if there is enough free memory on the HEAP. }
  51.   { If there is, then allocate buffer on the HEAP. }
  52.   if (maxavail > co_BuffSize) then
  53.     new(po_Buffer)
  54.   else
  55.   begin
  56.     close(fi_Temp);
  57.     writeln('ERROR: Insufficient HEAP memory!')
  58.   end;
  59.  
  60.   { Load file-data into buffer. }
  61.   blockread(fi_Temp, po_Buffer^, co_BuffSize, wo_BytesRead);
  62.   CheckForErrors(fi_Temp, true);
  63.  
  64.   { Display each byte that was read-in. }
  65.   for wo_BuffIndex := 1 to wo_BytesRead do
  66.     write(chr(po_Buffer^[wo_BuffIndex]));
  67.  
  68.   close(fi_Temp)
  69. END.
  70.